<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
$return['mid']=$_SESSION['uid'];
$return['show_setmobile']=cfc('core')->check_setmobile(1);
$return['show_setmember']=cfc('core')->check_setmember(1);
if($_RQ['op']=='main'){
	//去除匿名下单功能
	$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
	$shopset = $this->plugin_setting('shopset');
	//营销设置
	$saleset=$this->plugin_setting('saleset');
	$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$member2=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2'));
	$member=array_merge($member,$member2);
	$goodsid = intval($_RQ['goodsid']);
	//来自商户的订单
	$merchid = intval($_RQ['merchid']);
	if($_RQ['storeid']){
		$_SESSION['store_id']=intval($_RQ['storeid']);
	}
	$store_id=$_SESSION['store_id'];
	$sendtype=intval($_RQ['sendtype'])?:1;
	$main=array();
	$return['post']=array();
	if(!$merchid || $merchid<0){
			$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
			if($carrier_list){
				foreach($carrier_list as &$pv){
					$pv['pcd']=json_decode($pv['pcd'],true);
				}
				$return['carrier_list']=$carrier_list;
				$return['selectstore']=$carrier_list['0'];
			}
	}else{
			$return['merch'] = pdo_get('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$merchid));
	}
	if($goodsid){
		$return['post']['goodsid'] = $goodsid;
	}
	$merchs=$this->model('get_jiesuan')->get_merchs($return);
	if(empty($merchs)){
		exi('请先选择商品再下单！','error','jy_weishop/member.cart/');
	}
	$merchs=array_values($merchs);
	$merchpost=array();
	foreach($merchs as $k=>$mv){
		$merchpost[$k]=array();
		if($mv['sendtypes']){
			$return['sendtypes']=$mv['sendtypes'];
		}
		if($mv['issendtype'] && !$return['sendtypes']){
			$sendtype=1;
		}
	}
	//返回链接
	$return['forward_url']=base64_encode('jy_weishop/order.list//fromcreate=1');
	$return['citypeisong']=$this->plugin_setting('citypeisong');
	if($return['citypeisong']['isopen']){
		$main['ismake']=$return['citypeisong']['ismake'];
	}
	//点餐机制：
	$main['sendtype']=$sendtype?:$main['sendtype'];
	if($_SESSION['table_id']){
		$main['sendtype']=2;
	}
	$kuaipeisong=$this->plugin_setting('kuaipeisong');
	if($kuaipeisong['isnoopen']){
		$sendtype=$sendtype==1?2:$sendtype;
		unset($return['sendtypes'][1]);
	}
	if($return['citypeisong']['isopen']){
		$return['sendtypes']=array(
				'1'=>'同城配送',
				'2'=>'到店自取',
		);
	}
	if($_SESSION['is_dish']){
		$return['sendtypes']=array(
			'1'=>'外卖配送',
			'2'=>'到店自取',
		);
	}
	$return['sendtypes']=$return['sendtypes']?:array();
	//整理收货地址信息
	if($store_id){
		$return['selectstore']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	}
	if($return['selectstore']['id']){
		$return['selectstore']['juli']=1;
	}
	$get_address=$this->model('order/dispatch')->get_address($_RQ['addressid']);
	$get_address['address']['pcd']=json_decode($get_address['address']['pcd'],true);
	$main['address']=$get_address['address'];
	$main['addressid']=$get_address['addressid'];
	$return['address_list']=pdo_getall('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['address_item']=array();
	$return['main']=$main;
	$return['post']['invoice'] = $return['shopset']['invoice'];
	$return['post']['merchpost']=$merchpost;
	$return['post']['address'] = $main['address']?:array('city'=>'');
	$return['post']['addressid'] = $main['addressid'];
	$return['post']['sendtype'] = $sendtype?:1;
	$return['post']['sendtime'] = strtotime("+2 day");
	$return['post']['storeid'] = $return['selectstore']['id'];
	$return['post']['deliverytime'] = date("m月d日",strtotime("+2 day"));
	$return['post']['realname']=$return['post']['address']['name'];
	$return['post']['mobile']=$return['post']['address']['mobile'];
	$return['member']=$member;
	$this->session('merchs',$merchs);
	$return['merchs']=$merchs;
	set_html('title','确认订单');
	//代付
	$peerpay_setting=$this->plugin_setting('peerpay');
	if($peerpay_setting && $peerpay_setting['open'] && $main['endprice']>0){
		$return['peerpay']=true;
	}
	//获取小程序订阅消息ID
	$return['tmplIds']==array();
	$return['fromh5wxapp']=$_SESSION['fromh5wxapp'];
	$return['openid']=$_SESSION['openid'];
	$storeset=$this->plugin_setting('storeset');
	if($storeset['open_storeid'] && !$return['post']['merchid']){
		$return['showstore_no']=1;
		$return['store_notitle']=$storeset['store_notitle']?:'门店编号';
	}
	exi($return);
}

if($_RQ['op']=='juli'){
	//判断距离
		$post=get_postdata();
		$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
		$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
		if($l['lng'] && $l['lat']){
			$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
			if($d/1000>$post['juli']){
				exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
				exi('success');
			}else{
				exi('success');
			}
		}	
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	//整理订单数据
	$merchs=$this->session('merchs');
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core' && $_SESSION['current_plugin']!='jy_weishop'){
			$success_url=plugin($_SESSION['current_plugin'])->weishop_order_detail;
			if($success_url){
				$success_url=$_SESSION['current_plugin'].$success_url;
				$error_url=$success_url;
			}
			$post['success_url']=$success_url;
			$post['error_url']=$error_url;
	}
	$fee=0;
	$he_pay=array();
	foreach($merchs as $order){
		$postorder=$this->model('get_jiesuan')->postorder($order,$post);
		$order_id=$postorder['oid'];
		$payid=$postorder['pay_id'];
		$he_pay[]=$postorder;
		$fee +=$postorder['fee'];
		$plugin_xuni=$postorder['plugin_xuni'];
	}
	if(count($merchs)>1){
		$params=array(
				'id'=>-1,
				'title'=>'购买商品',
				'fee'=>$fee,
				'plugin_xuni'=>$plugin_xuni,
				'he_pay'=>$he_pay,
				'back_url'=>'jy_weishop/order.createall/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
				'success_url'=>$post['success_url']?:'jy_weishop/order.list/',
				'error_url'=>$post['error_url']?:'jy_weishop/order.list/',
		);
		$payid=cfc('pay')->create_pay_order($params);
	}else{
		if($post['peerpay']){
			$peerpay_setting=$this->plugin_setting('peerpay');
			if($peerpay_setting && $peerpay_setting['open']){
				exi('','','/order.peerpay//orderid='.$order_id);
			}
		}
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='get_freight'){
	$merchid=intval($_RQ['merchid']);
	$index=intval($_RQ['index']);
	$addressid=intval($_RQ['addressid']);
	$addressid=$addressid?:intval($_RQ['address']);
	if(!$addressid){
		exi('请先选择地址！','error');
	}
	if($merchid){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	}
	$merchs=$this->session('merchs');
	$order=$merchs[$index];
	$up_dispatch=$this->model('order/dispatch')->up_dispatch($order,array(),$addressid,$_SESSION['store_id'],$merch);
	$freight=$up_dispatch['get_freight'];
	//多少钱起送
	$freight['no_out_price']=$freight['no_out_price']?:0;
	if($_RQ['showbaoyou']){
		$freight['baoyou_man']=$freight['baoyou_man']?:0;
		exi($freight);
	}else{
		exi($freight['fee']);
	}	
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_weishop/order.list/');
	}
	$paylog=pdo_get('core_paylog',array('id'=>$payresult['pay_id']));
	$he_pay=json_decode($paylog['he_pay'],true);
	foreach($he_pay as $hv){
		$onepay=pdo_get('core_paylog',array('id'=>$hv['pay_id']));
		pdo_update('core_paylog',array('status'=>1,'pay_type'=>$paylog['pay_type']),array('id'=>$hv['pay_id']));
		$order=pdo_get('jy_weishop_order',array('id'=>$onepay['oid']));
		if($order['ispay'] ==1){
			exi('该订单已经支付！','error','jy_weishop/order.list/');
		}
		if($order['price']!=$onepay['fee']){
			exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$onepay['fee'],'error','jy_weishop/order.list/');
		}
		$params=array(
			'paytype'=>$paylog['pay_type'],
			'transid'=>$paylog['transaction_id'],
			'paytime'=>TIMESTAMP,
		);
		$this->model('order')->order_pay($order['id'],$params);
	}
	exi('提交成功!');
}

?>